var publishItemStatus:Object;
var deployItemCount:Number;
var deployItemTotal:Number;

function init(param, data, slot)
{
	Packages.model.Context.activeContent=this;

	/*// Condition.check
	if (Packages.model.UserProfile.RecordingCollabId==null) 
	{
		Packages.components.SceneAgent.loadScene("content/HomePage", true);
		return;
	}
	// */
	
	// UI.init
	var basic=Packages.system.mlf.GateKeeper.scanElement(this._name, "basic");
	Packages.system.display.JAccordion.initialize(basic);
	
	// Render.init
	Packages.system.render.Filter.titleFilter(Packages.system.mlf.GateKeeper.scanElement(this._name, "publishTitle"));
	Packages.system.render.Filter.titleFilter(Packages.system.mlf.GateKeeper.scanElement(this._name, "myReadjoyTitle"));
	Packages.system.display.SceneTransition.rollOut();

	// Event.init
	var evtTargetArr:Array=new Array();
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "Prepairation_print"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "OnAir_print"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "PostProduction_print"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "Publish_print"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "back_print"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "sendMsgSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "textFmtSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "mp3FmtSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "aacFmtSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "webFmtSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "swfFmtSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "zipFmtSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "playerSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "browserSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "recorderSpot"));
	evtTargetArr.push(Packages.system.mlf.GateKeeper.scanElement(this._name, "publishSpot"));
	Packages.system.events.StateFulButtonEvent.initialize(evtTargetArr);

	invokeInitComplete();
}

function dispose()
{
	Packages.system.mlf.GateKeeper.removeListener("onReleaseButtonEvent", this);
	Packages.system.mlf.GateKeeper.removeListener("onPublishReadingMaterial", this);

	Packages.modula.task.Common.stopCollabEnvironment();
}

function invokeInitComplete()
{
	Packages.system.mlf.GateKeeper.addListener("onReleaseButtonEvent", this);

	publishItemStatus=new Object();

	/*// select the default publishItem - deploy task
	var deploySpot=Packages.system.mlf.GateKeeper.scanElement(this._name, "textFmtSpot");
	publishItemStatus[deploySpot._name]=true;
	var param=mlf.mcmanager.PathResolver.drawPath(deploySpot);
	Packages.system.events.StateFulButtonEvent.invokeReleaseEvent(param, data, this._name);
	var deploySpot=Packages.system.mlf.GateKeeper.scanElement(this._name, "mp3FmtSpot");
	publishItemStatus[deploySpot._name]=true;
	var param=mlf.mcmanager.PathResolver.drawPath(deploySpot);
	Packages.system.events.StateFulButtonEvent.invokeReleaseEvent(param, data, this._name);
	var deploySpot=Packages.system.mlf.GateKeeper.scanElement(this._name, "webFmtSpot");
	publishItemStatus[deploySpot._name]=true;
	var param=mlf.mcmanager.PathResolver.drawPath(deploySpot);
	Packages.system.events.StateFulButtonEvent.invokeReleaseEvent(param, data, this._name);
	var deploySpot=Packages.system.mlf.GateKeeper.scanElement(this._name, "zipFmtSpot");
	publishItemStatus[deploySpot._name]=true;
	var param=mlf.mcmanager.PathResolver.drawPath(deploySpot);
	Packages.system.events.StateFulButtonEvent.invokeReleaseEvent(param, data, this._name);*/
	
	// check the collab env and initialize the env
	Packages.modula.task.Common.checkCollabEnvironment(this._name);
}

function publishReadingMaterial()
{
	Packages.system.mlf.GateKeeper.addListener("onPublishReadingMaterial", this);
	Packages.modula.task.Processor.publish(publishItemStatus);
}

function onPublishReadingMaterial(obj, dCount, dTotal, execFlag:Boolean)
{
	if (Packages.model.UserProfile.recordingCollabFlag && !execFlag)
	{
		var args=new Array();
		args.push(obj);
		args.push(deployItemCount);
		args.push(deployItemTotal);
		args.push(true);
		Packages.modula.task.Common.updateCollab(this, "onPublishReadingMaterial", args);
		return;
	}
	
	var rstHint=mlf.gatekeeper.native.Caller.scanElement("Publish", obj.scope+"FmtRst");
	rstHint.htmlText=Packages.system.mlf.GateKeeper.scanDictionary(rstHint, obj.result);
	deployItemCount++;
	var allHint=mlf.gatekeeper.native.Caller.scanElement("Publish", "allFmtRst");
	if (obj.scope=="all")
	{
		allHint.htmlText=Packages.system.mlf.GateKeeper.scanDictionary(allHint, obj.result);
	}
	else 
	{
		if (deployItemTotal==null) allHint.text=dCount+" / "+dTotal;
		else allHint.text=deployItemCount+" / "+deployItemTotal;
	}
}

function onReleaseButtonEvent(evt:Object, execFlag:Boolean)
{
	// Three types operation for collab env
	// private operation: sendMsgSpot(public), publishSpot(host)
	// public operation: none
	// host operation: FmtSpot
	
	if (evt.param.indexOf("FmtSpot")>-1)
	{
		if (Packages.model.UserProfile.recordingCollabFlag && !execFlag)
		{
			if (!Packages.modula.task.Composition.host)
			{
				Packages.modula.MessageCenter.send("YOU_HAVE_NO_RIGHTS_TO_DO_HOST_OPERATION", 1, 0);
				return;
			}

			var args=new Array();
			args.push(evt);
			args.push(true);
			Packages.modula.task.Common.updateCollab(this, "onReleaseButtonEvent", args);
			return;
		}

		if (publishItemStatus[evt.param])
		{
			publishItemStatus[evt.param]=false;
			var target=Packages.system.mlf.GateKeeper.scanElement(this._name, evt.param);
			Packages.system.events.StateFulButtonEvent.removeSelectedSpot(target);
		}
		else 
		{
			publishItemStatus[evt.param]=true;
			var target=Packages.system.mlf.GateKeeper.scanElement(this._name, evt.param);
			Packages.system.events.StateFulButtonEvent.createSelectedSpot(target);
		}
	}
	else if (evt.param=="publishSpot")
	{
		if (Packages.model.UserProfile.recordingCollabFlag && !Packages.modula.task.Composition.host)
		{
			Packages.modula.MessageCenter.send("YOU_HAVE_NO_RIGHTS_TO_DO_HOST_OPERATION", 1, 0);
			return;
		}

		Packages.modula.task.Common.sendMsg(evt.param.toUpperCase());
		
		deployItemCount=0;
		deployItemTotal=1;
		for (var a in publishItemStatus)
		{
			if (publishItemStatus[a]) deployItemTotal++;
		}
		publishReadingMaterial();
	}
	else if (evt.param=="sendMsgSpot")
	{
		Packages.modula.task.Common.sendMsg();
	}
}